<template>
  <div>
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="@/assets/logo.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="primary" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主题区域 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-row class="tac">
            <el-col :span="24">
              <el-menu router class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose" :collapse="isCollapse" :collapse-transition="false" :unique-opened="true" background-color="#333744" text-color="#fff" active-text-color="#ffd04b" width="200px" :default-active="activePath">
                <!-- 一级菜单 -->
                <el-submenu :index="'/' + item.path" v-for="item in menuList" :key="item.id">
                  <template slot="title">
                    <i :class="iconsObj[item.id]"></i>
                    <span>{{ item.authName }}</span>
                  </template>

                  <!-- 二级菜单 -->
                  <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="savaNavState('/' + subItem.path)">
                    <template slot="title">
                      <i class="el-icon-location"></i>
                      <span>{{ subItem.authName }}</span>
                    </template>
                  </el-menu-item>
                </el-submenu>
              </el-menu>
            </el-col>
          </el-row>
        </el-aside>
        <!-- 右侧主题 -->
        <el-container>
          <el-main>
            <!-- 路由占位符 -->
            <router-view> </router-view>
          </el-main>
          <el-footer>Footer</el-footer>
        </el-container>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-user ',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao',
      },
      //默认不折叠
      isCollapse: false,
      // 被激活的地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    console.log('aaaa')
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')

      //    this.router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      this.$message.success(res.meta.msg)

      console.log(res)
    },

    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    // 保存链接的激活状态
    savaNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = this.activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  color: #fff;
  line-height: 100%;
  font-size: 20px;
  padding: 0;
  align-items: center;
  div {
    display: flex;
    align-items: center;
  }
  img {
    margin: 0 10px;
    width: 50px;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: 2px solid rgb(31, 119, 190);
  }
}
.el-main {
  background-color: #eaedf1;
  // .el-menu {
  //   border-right: 2px solid blue;
  // }
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  background-color: #4a5064;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
